package com.yulongtian.interviewWorks.day20220823;

import java.util.Arrays;

/**
 * @author YulongTian
 * @date 2022/8/23 - 14:55
 */
public class InterviewsWork03 {
    public static void main(String[] args) {
        System.out.println(replaceSpaces("Mr John Smith    ",13));
    }

    public static String replaceSpaces(String S, int length) {
        String ss = S.substring(0, length);
        int blankCount = 0;
        for (int i = 0; i < length; i++) {
            if (ss.charAt(i) == ' ') {
                blankCount++;
            }
        }
        char[] chs = new char[length + blankCount * 2];
        char[] temp = new char[]{'%', '2', '0'};
        int addIndex = 0;
        for (int i = 0; i < length; i++) {
            if (ss.charAt(i) == ' ') {
                for (int j = 0; j < 3; j++) {
                    chs[i + addIndex] = temp[j];
                    addIndex++;
                }
                addIndex--;
            } else {
                chs[i + addIndex] = ss.charAt(i);
            }

        }
        StringBuilder res= new StringBuilder();
        for(int i=0;i<chs.length;i++){
            res.append(chs[i]);
        }

        return res.toString();
    }
}
